package com.jieshuibao.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.common.res.BaseRes;
import com.jieshuibao.model.db.UserSearch;
import com.jieshuibao.model.domain.OnlookReq;
import com.jieshuibao.service.CounselorService;
import com.jieshuibao.util.IKAnalyserUtil;
import com.jieshuibao.util.StringUtils;

/**
 * @author :liuxb@jieshuibao.com
 * @version ：2018年8月24日 上午9:36:05
 * 类说明 咨询师
 */
@Controller
@RequestMapping(value="/counselor")
public class CounselorController {
	
	@Resource
	private CounselorService CounselorService;
	
	//咨询师列表
	@RequestMapping(value="/coun/list",method=RequestMethod.POST)
	@ResponseBody
	public Object counselorList(HttpServletRequest request,@RequestBody(required=false)OnlookReq req){
		BaseRes<List<UserSearch>> res = new BaseRes<List<UserSearch>>();
		try {
			if (req==null) {
				res.setCode(BaseRes.ERRORPARAM_CODE);
				res.setDes(BaseRes.ERRORPARAM_DES);
				return res;
			}
			int sinceId = req.getSinceId();
			List<UserSearch> data = CounselorService.getCounselorList(req);
			res.setCode(BaseRes.SUCCESS_CODE);
			res.setDes(BaseRes.SUCCESS_DES);
			res.setData(data);
			res.setMaxId(sinceId+ data.size() - 1);
			res.setSinceId(sinceId);
			res.setRows(data.size());
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			res.setCode(BaseRes.FAIL_CODE);
			res.setDes(BaseRes.FAIL_DES);
			return res;
		}
		
	}
	
	@RequestMapping(value="/ikanaly/list",method=RequestMethod.POST)
	@ResponseBody
	public Object counselorListIkASerarch(HttpServletRequest request,@RequestBody(required=false)OnlookReq req){
		
		BaseRes<HashMap<String,Object>> res = new BaseRes<HashMap<String,Object>>();
		try {
			HashMap<String,Object> map = new HashMap<String, Object>();
			if (req==null) {
				res.setCode(BaseRes.ERRORPARAM_CODE);
				res.setDes(BaseRes.ERRORPARAM_DES);
				return res;
			}
			int sinceId = req.getSinceId();
			int maxId = req.getMaxId();
			String content = req.getContent();
			List<String> segment=null;
			if (StringUtils.isNotBlank(content)) {
				segment= IKAnalyserUtil.segment(content);
			}
			
			List<UserSearch> data=CounselorService.getCounselorList(segment, sinceId, maxId, null);
			map.put("segment", segment);
			map.put("UserSearch", data);
			res.setCode(BaseRes.SUCCESS_CODE);
			res.setDes(BaseRes.SUCCESS_DES);
			res.setData(map);
			res.setMaxId(sinceId+ data.size() - 1);
			res.setSinceId(sinceId);
			res.setRows(data.size());
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			res.setCode(BaseRes.FAIL_CODE);
			res.setDes(BaseRes.FAIL_DES);
			return res;
		}
		
	}


}
